//package com.openecommerce.service.impl;
//
//import java.text.DecimalFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//import javax.annotation.Resource;
//
//import com.openecommerce.domain.Carrier;
//import com.openecommerce.domain.OrderItem;
//import com.openecommerce.domain.OrderItemIdQty;
//import com.openecommerce.domain.OrderShipment;
//import com.openecommerce.domain.OrderShipmentComment;
//import com.openecommerce.domain.OrderShipmentItem;
//import com.openecommerce.domain.OrderShipmentTrack;
//import com.openecommerce.messagequeue.PublisherManager;
//import com.openecommerce.messagequeue.message.EmailMessage;
//import com.openecommerce.persistence.OrderItemMapper;
//import com.openecommerce.persistence.OrderShipmentCommentMapper;
//import com.openecommerce.persistence.OrderShipmentItemMapper;
//import com.openecommerce.persistence.OrderShipmentMapper;
//import com.openecommerce.persistence.OrderShipmentTrackMapper;
//import com.openecommerce.service.OrderShipmentService;
//import com.openecommerce.util.PropertyUtils;
//
//public class OrderShipmentServiceImpl implements OrderShipmentService {
//
//	@Resource
//	private OrderShipmentMapper orderShipmentMapper;
//	@Resource
//	private OrderShipmentCommentMapper orderShipmentCommentMapper;
//	@Resource
//	private OrderShipmentItemMapper orderShipmentItemMapper;
//	@Resource
//	private OrderShipmentTrackMapper orderShipmentTrackMapper;
//	@Resource
//	private OrderItemMapper orderItemMapper;
//	@Resource
//	PublisherManager publicManager;
//	
//	@Override
//	public List<OrderShipment> getList(Long orderId) {
//		return orderShipmentMapper.selectByOrderId(orderId);
//	}
//
//	@Override
//	public OrderShipment get(Long orderShipmentId) {
//		return orderShipmentMapper.selectByPrimaryKey(orderShipmentId);
//	}
//
//	@Override
//	public Long create(Long orderId, List<OrderItemIdQty> itemsQty, String comment,
//			String email, Boolean includeCommentInEmail) {
//		OrderShipment record = new OrderShipment();
//		record.setCreatedAt(new Date());
//		record.setOrderId(orderId);
//		Long orderShipmentId = orderShipmentMapper.insertSelective(record);
//		for(OrderItemIdQty item : itemsQty){
//			OrderItem orderItem = orderItemMapper.selectByPrimaryKey(item.getOrderItemId());
//			OrderShipmentItem orderShipmentItem = new OrderShipmentItem();
//			orderShipmentItem.setOrderShipmentId(orderShipmentId);
//			orderShipmentItem.setQty(item.getQty());
//			orderShipmentItem.setName(orderItem.getName());
//			orderShipmentItem.setOrderItemId(orderItem.getOrderItemId());
//			orderShipmentItem.setPrice(orderItem.getPrice());
//			orderShipmentItem.setProductId(orderItem.getProductId());
//			orderShipmentItem.setSku(orderItem.getSku());
//			orderShipmentItem.setWeight(orderItem.getWeight());
//			orderShipmentItemMapper.insertSelective(orderShipmentItem);
//		}
//		addComment(orderShipmentId, comment, email, includeCommentInEmail);
//		return orderShipmentId;
//	}
//
//	@Override
//	public boolean addComment(Long orderShipmentId, String comment, String email,
//			Boolean includeCommentInEmail) {
//		OrderShipmentComment orderShipmentComment = new OrderShipmentComment(); 
//		orderShipmentComment.setOrderShipmentId(orderShipmentId);
//		orderShipmentComment.setCreatedAt(new Date());
//		orderShipmentComment.setComment(comment);
//		int result = orderShipmentCommentMapper.insertSelective(orderShipmentComment);
//		EmailMessage message = new EmailMessage();
//		message.setEmail(email);
//		//add template
//		if(includeCommentInEmail){
//			message.setContent(comment);
//		}
//		publicManager.publish(message);
//		return result == 1?true:false;
//	}
//
//	@Override
//	public int addTrack(Long orderShipmentId, String carrierCode, String title,
//			String trackNumber) {
//		OrderShipmentTrack record = new OrderShipmentTrack();
//		record.setCreatedAt(new Date());
//		record.setTitle(title);
//		record.setOrderShipmentId(orderShipmentId);
//		//record.setOrderId()
//		//record.setNumber(number)
//		record.setCarrierCode(carrierCode);
//		return orderShipmentTrackMapper.insertSelective(record);
//	}
//
//	@Override
//	public boolean removeTrack(Long orderShipmentTrackId) {
//		int result = orderShipmentTrackMapper.deleteByPrimaryKey(orderShipmentTrackId);
//		return result == 1 ?true:false;
//	}
//
//	@Override
//	public List<Carrier> getCarriers() {
//		List<Carrier> carriers = new ArrayList<Carrier>();
//		DecimalFormat df = new DecimalFormat("00");
//		for(int i=1;;i++){
//			String code = PropertyUtils.getString("carrier"+df.format(i)+".code");
//			if(code == null){
//				break;
//			}
//			String name = PropertyUtils.getString("carrier"+df.format(i)+".name");
//			Carrier carrier = new Carrier();
//			carrier.setCode(code);
//			carrier.setName(name);
//			carriers.add(carrier);
//		}
//		return carriers;
//	}
//
//}
